﻿#include "SelectView.h"
#include"../PublicHead.h"

extern GameConfig gameconfig;
void SelectView::init()
{
	loadimage(&image, "./resources/images/selectview.jpg", 1000, 750);
	Button* a=new Button(340, 250,460,290,"经典模式");	//经典模式
	Button* a1 = new Button(570,250,690,290,"限时模式");	//其他
	Button* a2= new Button(350,400,410,440,"简单");	//简单
	Button* a3 = new Button(500,400,560,440,"普通");	//普通
	Button* a4 = new Button(650,400,710,440,"困难");	//困难
	Button* a5 = new Button(350, 600, 475, 640, "开始游戏");	//开始游戏
	Button* a11 = new Button(580, 600, 705, 640, "返回菜单");	//返回菜单
	Button* a6 = new Button(750, 400, 830, 440, "自定义");	//自定义
	Button* a7 = new Button(860, 500, 890, 550, "+");	//延迟掉落速度变大
	Button* a8 = new Button(730, 500, 760, 550, "-");	//延迟掉落速度减小
	Button* a9 = new Button(730, 550, 760, 600, "-");	//方块变小
	Button* a10 = new Button(860, 550, 890, 600,"+");	//方块变大
	my_bottons.push_back(a);
	my_bottons.push_back(a1);
	my_bottons.push_back(a2);
	my_bottons.push_back(a3);
	my_bottons.push_back(a4);
	my_bottons.push_back(a5);
	my_bottons.push_back(a6);
	my_bottons.push_back(a7);
	my_bottons.push_back(a8);
	my_bottons.push_back(a9);
	my_bottons.push_back(a10);
	my_bottons.push_back(a11);
}
void SelectView::handleEvent()
{

	InputHandler han;
	han.GetMsg();
	//开始游戏按钮被点击，判断游戏模式切换界面
	if (my_bottons[5]->IsClicked(han))
	{
		//切换模式为经典模式
		if (count_2 == 1)
		{
			game.cur_view_ = GameView::CLASSICVIEW;
		}
		else if (count_2 == 2)
		{
			//切换模式为经典模式
			game.cur_view_ = GameView::LIMITTIMEVIEW;
		}
	}
	if (my_bottons[11]->IsClicked(han))
	{
		//返回菜单按钮被点击，则切换当前界面
		game.cur_view_ = GameView::MENUVIEW;
	}
	if (my_bottons[0]->IsClicked(han))
	{
		//经典模式按钮被点击，则切换模式为经典模式
		count_2 = 1;
	}
	if (my_bottons[1]->IsClicked(han))
	{
		//限时模式按钮被点击，则切换模式为限时模式
		count_2 = 2;
	}
	if(my_bottons[2]->IsClicked(han))
	{
		//容易
		count = 1;
		//设置游戏难度
		game.now_difficulty_ = Difficulty::EASY;
		blocksize = 1;
		gameconfig.changBlocksize(BlockSize::SMALL);
		gameconfig.changespeed(1000);
		
	}
	if (my_bottons[3]->IsClicked(han))
	{
		//普通
		count = 2;
		game.now_difficulty_ = Difficulty::NORMAL;
		blocksize = 2;
		gameconfig.changBlocksize(BlockSize::MIDDLE);
		gameconfig.changespeed(500);
	}
	if (my_bottons[4]->IsClicked(han))
	{
		//困难
		count = 3;
		game.now_difficulty_ = Difficulty::HARD;
		blocksize = 3;
		gameconfig.changBlocksize(BlockSize::BIG);
		gameconfig.changespeed(300);
	}
	if (my_bottons[6]->IsClicked(han))
	{
		count = 4;
		//自定义按钮被点击，进入自定义模式
	}
	if (count == 4 && my_bottons[7]->IsClicked(han))
	{
		//增加下落时间延迟
		gameconfig.dropSpeed += 100;
		if (gameconfig.dropSpeed >1000)
		{
			gameconfig.dropSpeed = 1000;
		}
	}
	if (count == 4 && my_bottons[8]->IsClicked(han))
	{
		//减少下落时间延迟
		gameconfig.dropSpeed -= 100;
		if (gameconfig.dropSpeed < 300)
		{
			gameconfig.dropSpeed = 300;
		}
	}
	if (count == 4 && my_bottons[9]->IsClicked(han))
	{
		//减小方块大小
		blocksize--;
		if (blocksize==0)
		{
			blocksize = 1;
			gameconfig.changBlocksize(BlockSize::SMALL);
		}
		if(blocksize==1)
		{
			gameconfig.changBlocksize(BlockSize::SMALL);
		}
		if (blocksize == 2)
		{
			gameconfig.changBlocksize(BlockSize::MIDDLE);
		}
	}
	if (count == 4 && my_bottons[10]->IsClicked(han))
	{
		//增加方块大小
		blocksize++;
		if (blocksize == 4)
		{
			blocksize = 3;
			gameconfig.changBlocksize(BlockSize::BIG);
		}
		if (blocksize == 3)
		{
			gameconfig.changBlocksize(BlockSize::BIG);
		}
		if (blocksize == 2)
		{
			gameconfig.changBlocksize(BlockSize::MIDDLE);
		}
	}
	//根据选项进行绘图
	switch (count_2)
	{
	case(1):
		setfillcolor(GREEN);
		fillcircle(400, 310, 10);
		break;
	case(2):
		setfillcolor(GREEN);
		fillcircle(630, 310, 10);
		break;
	default:
		break;
	}
	switch (count)
	{
	case(1):
		setfillcolor(GREEN);
		fillcircle(375, 470, 10);
		break;
	case(2):
		setfillcolor(GREEN);
		fillcircle(525, 470, 10);
		break;
	case(3):
		setfillcolor(GREEN);
		fillcircle(675, 470, 10);
		break;
	case(4):
		setbkcolor(WHITE);
		settextstyle(30, 0, _T("微软雅黑"));
		outtextxy(770, 500, _T("延迟速度"));
		outtextxy(770, 550, _T("方块大小"));
		settextstyle(30, 0, _T("微软雅黑"));
		for (int i = 7; i < my_bottons.size()-1; i++)
		{
				my_bottons[i]->DrawRuntaButton();
		}
		break;
	default:
		break;
	}
	flushmessage(EX_MOUSE);
}
void SelectView::update()
{

}
void SelectView::render()
{
	putimage(0, 0, &image);
	for (int i = 0; i < my_bottons.size(); i++)
	{
		if(i<=6||i==11)
		my_bottons[i]->DrawRuntaButton();
	}

	settextcolor(WHITE);
	settextstyle(40, 0, _T("微软雅黑"));
	outtextxy(200, 250, _T("模式"));
	outtextxy(200, 400, _T("难度"));
	settextstyle(40, 0, _T("微软雅黑"));
	outtextxy(650, 90, _T("目前方块大小:"));
	outtextxy(650, 140, _T("目前延迟掉落速度:"));
	TCHAR str2[20];
	_itot_s(gameconfig.dropSpeed, str2, 10);
	outtextxy(920, 140, _T(str2));
	if (gameconfig.blockSize == (int)BlockSize::SMALL)
	{
		outtextxy(860, 90, _T("SMALL"));
	}
	else if (gameconfig.blockSize == (int)BlockSize::MIDDLE)
	{
		outtextxy(860, 90, _T("MIDDLE"));
	}
	else if(gameconfig.blockSize == (int)BlockSize::BIG)
	{
		outtextxy(860, 90, _T("BIG"));
	}


	

}
void SelectView::clean()
{
	my_bottons.clear();
}
void SelectView::run()
{
	init();
	while (game.cur_view_==GameView::SELECTVIEW)
	{
		BeginBatchDraw();
		render();
		handleEvent();
		EndBatchDraw();
	}
	clean();
}
